package com.css.fxfzaqbz.common.utils.cluster;

public class PointToPixelUtil {

    public static Pixel pointToPixel(Point point, int zoom) {
        double x = lngToPixel(point.lng, zoom), y = latToPixel(point.lat, zoom);
        return new Pixel(x, y);
    }

    public static Point pixelToPoint(Pixel pixel, int zoom) {
        double lng = pixelToLng(pixel.x, zoom), lat = pixelToLat(pixel.y, zoom);
        return new Point(lng, lat);
    }

    //经度到像素X值
    public static double lngToPixel(double lng, int zoom) {
        return (lng + 180) * (256  * 2^zoom) / 360;
    }

    //像素X到经度
    public static double pixelToLng(double pixelX, int zoom) {
        return pixelX * 360 / (256 *  2^ zoom) - 180;
    }

    //纬度到像素Y
    public static double latToPixel(double lat, int zoom) {
        double siny = Math.sin(lat * Math.PI / 180);
        double y = Math.log((1 + siny) / (1 - siny));
        return (128 * 2^ zoom) * (1 - y / (2 * Math.PI));
    }

    //像素Y到纬度
    public static double pixelToLat(double pixelY, int zoom) {
        double y = 2 * Math.PI * (1 - pixelY / (128  * 2 ^  zoom));
        double z = Math.pow(Math.E, y);
        double siny = (z - 1) / (z + 1);
        return Math.asin(siny) * 180 / Math.PI;
    }

    public static void main(String[] args) {
        // 330,174 (center: 116.40769,39.89945 | size: 754,400)
        // sw: 116.27827,39.84677; ne: 116.53713,39.95212
        // lt: 116.27827,39.95212
        Point point = new Point(116.39191,39.90605);
        Pixel pixel = PointToPixelUtil.pointToPixel(point, 12);
        //System.out.println(pixel.x + ", " + pixel.y);

        Point lt = new Point(116.27827,39.95212);
        Pixel ltPixel = PointToPixelUtil.pointToPixel(lt, 12);
        System.out.println(ltPixel.x + ", " + ltPixel.y);

        System.out.println((pixel.x - ltPixel.x) + ", " + (pixel.y - ltPixel.y));

        //Point position = PointToPixelUtil.pixelToPoint(pixel, 12);
        //System.out.println(position.lng + ", " + position.lat);
    }


}
